package com.book.controller;


import com.book.pojo.ChapterContent;
import com.book.pojo.Records;
import com.book.service.RecordsService;
import com.book.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxy
 * @since 2023-10-16 11:59:51
 */
@RestController
@RequestMapping("/records")
public class RecordsController {

    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private RecordsService recordsService;

    //根据读者id查询全部阅读历史记录
    @GetMapping("/getAllRecord")
    public List<Records> getAllRecord(@RequestHeader String unauthoriazed){
        Claims claims = jwtUtil.parseJwt(unauthoriazed);
        Long id = Long.valueOf(claims.getId());
        return recordsService.selectByReaderId(id);
    }

    //定位上一次阅读位置
    @GetMapping("/readerHistory")
    public ChapterContent readerHistory(@RequestHeader String unauthoriazed, Records records){
        String id = jwtUtil.parseJwt(unauthoriazed).getId();
        records.setReaderId(Long.valueOf(id));
        return recordsService.getHistory(records);
    }
}
